//
//  BottomDoubleBtnView.m
//  KuWoCan
//
//  Created by hwd on 17/3/16.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "BottomDoubleBtnView.h"
#import "KwAlertView.h"
#import "ChooseProsecutorCell.h"
#import "NwOrderRequest.h"
#import "NWSaleApplyRequest.h"

@interface BottomDoubleBtnView ()

@property (nonatomic,strong) NSIndexPath *lastIndexPath;

@property (nonatomic,strong) UIAlertView *refuseAlert;

@property (nonatomic,strong) UIAlertView *passAlert;

@end

@implementation BottomDoubleBtnView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init
{
    if (self = [super init])
    {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    self.backgroundColor = KwBgColor;
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.layer.masksToBounds = YES;
    leftBtn.layer.cornerRadius = 5;
    leftBtn.tag = 100;
    leftBtn.titleLabel.font = KwFont(15);
    [leftBtn setTitle:@"拒绝" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    leftBtn.backgroundColor = KwColor(@"#ff8a89");
    [leftBtn addTarget:self action:@selector(actionWithBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftBtn];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.layer.masksToBounds = YES;
    rightBtn.layer.cornerRadius = 5;
    rightBtn.tag = 101;
    rightBtn.titleLabel.font = KwFont(15);
    [rightBtn setTitle:@"通过" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.backgroundColor = KwBlueColor;
    [rightBtn addTarget:self action:@selector(actionWithBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightBtn];
    
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.bottom.equalTo(@-8);
        make.top.equalTo(@8);
        make.right.equalTo(rightBtn.mas_left).offset(-20);
    }];
    
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-20);
        make.bottom.equalTo(leftBtn.mas_bottom);
        make.top.equalTo(leftBtn.mas_top);
        make.width.equalTo(leftBtn.mas_width);
    }];
}

- (void)actionWithBtn:(UIButton *)btn
{
    
    if (btn.tag == 100)
    {
        //拒绝
        _refuseAlert = [[UIAlertView alloc] initWithTitle:@"拒绝" message:@"请输入拒绝原因" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
        [_refuseAlert setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [_refuseAlert show];
    }
    else
    {
        
        _passAlert = [[UIAlertView alloc] initWithTitle:@"通过" message:@"是否通过" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
        [_passAlert setAlertViewStyle:UIAlertViewStyleDefault];
        [_passAlert show];
    }
    
}

//获取弹出框文本
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (alertView == _refuseAlert)
    {
        UITextField *txt = [alertView textFieldAtIndex:0];
        if (buttonIndex == 1)
        {

            NSRange range = [txt.text rangeOfString:@"\\S+" options:NSRegularExpressionSearch];
            if (range.location == NSNotFound) {
                [SVProgressHUD showErrorWithStatus:@"请填写拒绝原因"];
                return;
            }

            if (_moduleType == HomeModuleTypeOrder)
            {
                NwOrderRequest *request = [[NwOrderRequest alloc] init];
                [request startRequestPassOrdersWithOrderId:_orderId userId:[Singleton shareInstance].userId orderStatus:_orderStatus approvalType:@"1" refuseReason:txt.text paymentProof:@""];
                [request completion:^(id result, NSString *message, id extension, BOOL succ) {
                    if (succ)
                    {
                        [SVProgressHUD showSuccessWithStatus:@"已拒绝"];
                        [[CommonTool getCurrtenViewCtrWithView:self].navigationController popViewControllerAnimated:YES];
                    }
                }];
            }
            else
            {
                NWSaleApplyRequest *request = [[NWSaleApplyRequest alloc] init];
                [request startRequestPassApplyOrCheckWithId:_orderId userId:[Singleton shareInstance].userId approvalStatus:_orderStatus approvalType:@"1" approvalMemo:txt.text moduleType:_moduleType MbContent:_mbContent];
                [request completion:^(id result, NSString *message, id extension, BOOL succ) {
                    if (succ)
                    {
                        [SVProgressHUD showSuccessWithStatus:@"已拒绝"];
                        [[CommonTool getCurrtenViewCtrWithView:self].navigationController popViewControllerAnimated:YES];
                    }
                }];
            }
            
        }
    }
    else
    {
        if (buttonIndex == 1)
        {
            if (_moduleType == HomeModuleTypeOrder)
            {
                NwOrderRequest *request = [[NwOrderRequest alloc] init];
                [request startRequestPassOrdersWithOrderId:_orderId userId:[Singleton shareInstance].userId orderStatus:_orderStatus approvalType:@"0" refuseReason:@"" paymentProof:@""];
                [request completion:^(id result, NSString *message, id extension, BOOL succ) {
                    if (succ)
                    {
                        [SVProgressHUD showSuccessWithStatus:@"已通过"];
                        [[CommonTool getCurrtenViewCtrWithView:self].navigationController popViewControllerAnimated:YES];
                    }
                }];
            }
            else
            {
                NWSaleApplyRequest *request = [[NWSaleApplyRequest alloc] init];
                [request startRequestPassApplyOrCheckWithId:_orderId userId:[Singleton shareInstance].userId approvalStatus:_orderStatus approvalType:@"0" approvalMemo:@"" moduleType:_moduleType MbContent:_mbContent];
                
                [request completion:^(id result, NSString *message, id extension, BOOL succ) {
                    if (succ)
                    {
                        [SVProgressHUD showSuccessWithStatus:@"已通过"];
                        [[CommonTool getCurrtenViewCtrWithView:self].navigationController popViewControllerAnimated:YES];
                    }
                }];
            }
        }
    }
}


- (UITableView *)createPeopleListView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    return tableView;
}

- (UITextView *)createTxtView
{
    UITextView *txtView = [UITextView new];
    txtView.backgroundColor = KwGaryColor;
    txtView.text = @"请填写拒绝原因";
    return txtView;
}


#pragma mark tableViewDelegate  DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cell_Id = @"ChooseProsecutorCell";
    ChooseProsecutorCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_Id];
    if (!cell)
    {
        cell = [[ChooseProsecutorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_Id];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当前cell row
    NSInteger newRow = [indexPath row];
    //记录上一次cell row
    NSInteger oldRow = (self.lastIndexPath != nil) ? [self.lastIndexPath row] : -1;
    if (newRow != oldRow)
    {
        //选中cell
        ChooseProsecutorCell *newcell =  [tableView cellForRowAtIndexPath:indexPath];
        newcell.selected = YES;
        //取消上一次选中cell
        ChooseProsecutorCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
        oldCell.selected = NO;
    }
    self.lastIndexPath = indexPath;
}

@end
